//
//  EvaluationController.m
//  Japanese+
//
//  Created by ccdaguo on 15/11/1.
//  Copyright © 2015年 SSIC. All rights reserved.
//

#import "EvaluationViewController.h"
#import "StudentCommentCell.h"
#import "TeacherCommentCell.h"
#import "EDStarRating.h"

#import "CommentModel.h"


@interface EvaluationViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation EvaluationViewController
{
    
    CommentModel *_commentModel;
    NSMutableArray *_dataArray;
    UserInfoManager *_userInfo;
    VOSegmentedControl *_segmentControl;
    VOSegmentedControl *_segment;

    UIView *_firstView;
    UIView *_secondView;
    
    UIView *_topView;
    EDStarRating *_totalStar;
    EDStarRating *_descStar;
    EDStarRating *_timeStar;
    EDStarRating *_clothStar;
    EDStarRating *_behaviorStar;
    UILabel *_commentPeople;
    UILabel *_totalScore;
    
    UITableView *_firstTable;
    UITableView *_secondTable;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"评价"];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];
    
    
    _userInfo = [UserInfoManager sharedUserInfoManager];
    [_userInfo loadUserInfoFromSanbox];
    [self creatSegmentController];
}

- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)creatSegmentController
{
    _segmentControl = [[VOSegmentedControl alloc] initWithSegments:@[@{
                                                                  @"text":@"老师对学生评价"
                                                                  },@{
                                                                  @"text":@"学生对老师评价"
                                                                  }]];
    _segmentControl.frame = CGRectMake(0, 64, ECSCREEN_W, 40);
    _segmentControl.animationType = VOSegCtrlAnimationTypeSmooth;
    _segmentControl.contentStyle = VOContentStyleTextAlone;
    _segmentControl.indicatorStyle = VOSegCtrlIndicatorStyleBottomLine;
    _segmentControl.allowNoSelection = NO;
    _segmentControl.scrollBounce = NO;
    _segmentControl.selectedTextFont = [UIFont systemFontOfSize:15];
    _segmentControl.selectedIndicatorColor = NAVC_COLOR;
    _segmentControl.textColor = ECCOLOR(0, 0, 0, 1);
    _segmentControl.selectedTextColor = NAVC_COLOR;
    _segmentControl.selectedBackgroundColor = ECCOLOR(255, 255, 255, 1);
    _segmentControl.backgroundColor = ECCOLOR(255, 255, 255, 255);
    _segmentControl.indicatorThickness = 2;
    _segmentControl.indicatorCornerRadius = 15;
    [_segmentControl addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segmentControl];
    [self segmentClick:_segmentControl];
}


- (void)segmentClick:(VOSegmentedControl *)segment
{
    if (segment == _segmentControl) {
        switch (_segmentControl.selectedSegmentIndex) {
            case 0:
            {
                if (_secondView) {
                    [_secondView removeFromSuperview];
                }
                [self creatFirstView];
                [_firstTable reloadData];
            }
                break;
            case 1:
            {
                if (_firstView) {
                    [_firstView removeFromSuperview];
                }
                [self creatSecondView];
                [_secondTable reloadData];
            }
                break;
                
            default:
                break;
        }
    }else{
        [_secondTable reloadData];
    }
}



- (void)creatHttpRequest
{
    NSDictionary *param = @{
                            @"t_member_id":_userInfo.memberModel.member_Id,
                            @"pageIndex":@"0",
                            @"pageSize":@"999",
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(SCHEDULECOMMENT) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [CommentModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"CommentData",
                         };
            }];
            
            if (_dataArray) {
                [_dataArray removeAllObjects];
            }else{
                _dataArray = [[NSMutableArray alloc] init];
            }
            
            _commentModel = [[CommentModel alloc] init];
            _commentModel = [CommentModel objectWithKeyValues:result];
            [_dataArray addObjectsFromArray:_commentModel.data];
            [_secondTable reloadData];
        }
    }];
}


- (void)creatFirstView
{
    _firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 104, ECSCREEN_W, ECSCREEN_H - 104)];
    _firstView.backgroundColor = BG_COLOR;
    [self.view addSubview:_firstView];
    
    _firstTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 1, ECSCREEN_W, ECSCREEN_H - 105) style:UITableViewStylePlain];
    _firstTable.delegate = self;
    _firstTable.dataSource = self;
    
    _firstTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    _firstTable.showsVerticalScrollIndicator = NO;

    _firstTable.tableFooterView = [[UIView alloc]init];
    
    if ([_firstTable respondsToSelector:@selector(setSeparatorInset:)]) {
        [_firstTable setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_firstTable respondsToSelector:@selector(setLayoutMargins:)]) {
        [_firstTable setLayoutMargins:UIEdgeInsetsZero];
    }
    
    [_firstView addSubview:_firstTable];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90.f;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _firstTable) {
        return 5;
    }else{
        switch (_segment.selectedSegmentIndex) {
            case 0:
            {
                return _dataArray.count;
            }
                break;
            case 1:
            {
                return 5;
            }
                break;
            case 2:
            {
                return 5;
            }
                break;
            case 3:
            {
                return 5;
            }
                break;

                
            default:
                break;
        }
        return _dataArray.count;
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _firstTable) {
        static NSString *reuseIdentifier = @"teacherCommentCell";
        TeacherCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (nil == cell) {
            NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"TeacherCommentCell" owner:nil options:nil];
            for (NSObject *obj in arr) {
                if ([obj isKindOfClass:[TeacherCommentCell class]]) {
                    cell = (TeacherCommentCell *)obj;
                    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                        [cell setSeparatorInset:UIEdgeInsetsZero];
                    }
                    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                        [cell setLayoutMargins:UIEdgeInsetsZero];
                    }
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    break;
                }
            }
        }
        
        [cell setCellWithModel:nil WithIndexPath:indexPath];
        return cell;
    }else{
        static NSString *reuseIdentifier = @"studentCommentCell";
        StudentCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (nil == cell) {
            NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"StudentCommentCell" owner:nil options:nil];
            for (NSObject *obj in arr) {
                if ([obj isKindOfClass:[StudentCommentCell class]]) {
                    cell = (StudentCommentCell *)obj;
                    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                        [cell setSeparatorInset:UIEdgeInsetsZero];
                    }
                    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                        [cell setLayoutMargins:UIEdgeInsetsZero];
                    }
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    break;
                }
            }
        }
        
        [cell setCellWithModel:_dataArray WithIndexPath:indexPath WithSegment:_segment.selectedSegmentIndex];
        return cell;
    }
}


- (void)creatSecondView
{
    _secondView = [[UIView alloc] initWithFrame:CGRectMake(0, 104, ECSCREEN_W, ECSCREEN_H - 104)];
    _secondView.backgroundColor = BG_COLOR;
    [self.view addSubview:_secondView];
    
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 1, ECSCREEN_W, 149)];
    _topView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [_secondView addSubview:_topView];
    
    _totalScore = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 150, 40)];
    _totalScore.text = @"5.0分";
    _totalScore.textAlignment = NSTextAlignmentCenter;
    _totalScore.font = [UIFont systemFontOfSize:20.f];
    [_topView addSubview:_totalScore];
    
    _totalStar = [[EDStarRating alloc] initWithFrame:CGRectMake(10, 55, 150, 40)];
    _totalStar.starImage = ECIMAGENAME(@"star_gray_big.png");
    _totalStar.starHighlightedImage = ECIMAGENAME(@"star_orange_big.png");
    _totalStar.maxRating = 5.0;
    _totalStar.horizontalMargin = 12.0;
    _totalStar.editable = NO;
    _totalStar.displayMode = EDStarRatingDisplayFull;
    _totalStar.rating = 3;
    _totalStar.backgroundColor = [UIColor clearColor];
    [_topView addSubview:_totalStar];
    
    
    _commentPeople = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 150, 40)];
    _commentPeople.text = @"共5人评论";
    _commentPeople.textAlignment = NSTextAlignmentCenter;
    _commentPeople.font = [UIFont systemFontOfSize:13.f];
    [_topView addSubview:_commentPeople];
    
    
    UILabel *desc = [[UILabel alloc] initWithFrame:CGRectMake(ECSCREEN_W/2, 20, 55, 20)];
    desc.text = @"描述相符";
    desc.textAlignment = NSTextAlignmentCenter;
    desc.font = [UIFont systemFontOfSize:13.f];
    [_topView addSubview:desc];
    
    _descStar = [[EDStarRating alloc] initWithFrame:CGRectMake(ECSCREEN_W/2 +55, 20, 80, 20)];
    _descStar.starImage = ECIMAGENAME(@"star_gray.png");
    _descStar.starHighlightedImage = ECIMAGENAME(@"star_orange.png");
    _descStar.maxRating = 5.0;
    _descStar.horizontalMargin = 12.0;
    _descStar.editable = NO;
    _descStar.displayMode = EDStarRatingDisplayFull;
    _descStar.rating = 3;
    _descStar.backgroundColor = [UIColor clearColor];
    [_topView addSubview:_descStar];

    
    /*
     EDStarRating *_descStar;
     EDStarRating *_timeStar;
     EDStarRating *_clothStar;
     EDStarRating *_behaviorStar;

     */
    UILabel *behavior = [[UILabel alloc] initWithFrame:CGRectMake(ECSCREEN_W/2, 50, 55, 20)];
    behavior.text = @"教学态度";
    behavior.textAlignment = NSTextAlignmentCenter;
    behavior.font = [UIFont systemFontOfSize:13.f];
    [_topView addSubview:behavior];
    
    
    _behaviorStar = [[EDStarRating alloc] initWithFrame:CGRectMake(ECSCREEN_W/2 +55, 50, 80, 20)];
    _behaviorStar.starImage = ECIMAGENAME(@"star_gray.png");
    _behaviorStar.starHighlightedImage = ECIMAGENAME(@"star_orange.png");
    _behaviorStar.maxRating = 5.0;
    _behaviorStar.horizontalMargin = 12.0;
    _behaviorStar.editable = NO;
    _behaviorStar.displayMode = EDStarRatingDisplayFull;
    _behaviorStar.rating = 3;
    _behaviorStar.backgroundColor = [UIColor clearColor];
    [_topView addSubview:_behaviorStar];

    
    UILabel *time = [[UILabel alloc] initWithFrame:CGRectMake(ECSCREEN_W/2, 80, 55, 20)];
    time.text = @"上课准时";
    time.textAlignment = NSTextAlignmentCenter;
    time.font = [UIFont systemFontOfSize:13.f];
    [_topView addSubview:time];
    
    
    _timeStar = [[EDStarRating alloc] initWithFrame:CGRectMake(ECSCREEN_W/2 +55, 80, 80, 20)];
    _timeStar.starImage = ECIMAGENAME(@"star_gray.png");
    _timeStar.starHighlightedImage = ECIMAGENAME(@"star_orange.png");
    _timeStar.maxRating = 5.0;
    _timeStar.horizontalMargin = 12.0;
    _timeStar.editable = NO;
    _timeStar.displayMode = EDStarRatingDisplayFull;
    _timeStar.rating = 3;
    _timeStar.backgroundColor = [UIColor clearColor];
    [_topView addSubview:_timeStar];

    
    UILabel *cloth = [[UILabel alloc] initWithFrame:CGRectMake(ECSCREEN_W/2, 110, 55, 20)];
    cloth.text = @"着装整洁";
    cloth.textAlignment = NSTextAlignmentCenter;
    cloth.font = [UIFont systemFontOfSize:13.f];
    [_topView addSubview:cloth];
    
    
    _clothStar = [[EDStarRating alloc] initWithFrame:CGRectMake(ECSCREEN_W/2 +55, 110, 80, 20)];
    _clothStar.starImage = ECIMAGENAME(@"star_gray.png");
    _clothStar.starHighlightedImage = ECIMAGENAME(@"star_orange.png");
    _clothStar.maxRating = 5.0;
    _clothStar.horizontalMargin = 12.0;
    _clothStar.editable = NO;
    _clothStar.displayMode = EDStarRatingDisplayFull;
    _clothStar.rating = 3;
    _clothStar.backgroundColor = [UIColor clearColor];
    [_topView addSubview:_clothStar];

    _segment = [[VOSegmentedControl alloc] initWithSegments:@[@{
                                                                         @"text":@"全部"
                                                                         },@{
                                                                         @"text":@"好评"
                                                                         },@{
                                                                         @"text":@"中评"
                                                                         },@{
                                                                         @"text":@"差评"
                                                                         }]];
    _segment.frame = CGRectMake(0, 151, ECSCREEN_W, 39);
    _segment.animationType = VOSegCtrlAnimationTypeSmooth;
    _segment.contentStyle = VOContentStyleTextAlone;
    _segment.indicatorStyle = VOSegCtrlIndicatorStyleBottomLine;
    _segment.allowNoSelection = NO;
    _segment.scrollBounce = NO;
    _segment.selectedTextFont = [UIFont systemFontOfSize:15];
    _segment.selectedIndicatorColor = NAVC_COLOR;
    _segment.textColor = ECCOLOR(0, 0, 0, 1);
    _segment.selectedTextColor = NAVC_COLOR;
    _segment.selectedBackgroundColor = ECCOLOR(255, 255, 255, 1);
    _segment.backgroundColor = ECCOLOR(255, 255, 255, 255);
    _segment.indicatorThickness = 2;
    _segment.indicatorCornerRadius = 15;
    [_segment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
    [_secondView addSubview:_segment];
    [self segmentClick:_segment];
    
    _secondTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 191, ECSCREEN_W, ECSCREEN_H - 150 - 40 - 64 - 40) style:UITableViewStylePlain];
    
    _secondTable.delegate = self;
    _secondTable.dataSource = self;
    _secondTable.showsVerticalScrollIndicator = NO;

    _secondTable.tableFooterView = [[UIView alloc]init];
    
    if ([_secondTable respondsToSelector:@selector(setSeparatorInset:)]) {
        [_secondTable setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_secondTable respondsToSelector:@selector(setLayoutMargins:)]) {
        [_secondTable setLayoutMargins:UIEdgeInsetsZero];
    }
    
    [_secondView addSubview:_secondTable];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
